package com.jy.adscm.operation.cg.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.QueryCgMaterial;
import com.jy.adscm.operation.cg.service.CgMaterialService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 基础物料表 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-01
 */
@RestController
@RequestMapping("adscm/operation/cg/cgMaterial")
public class CgMaterialController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    CgMaterialService cgMaterialService;

    /**
     * 增加bom信息
     * @param cgMaterial
     * @param request
     * @return
     */
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody CgMaterial cgMaterial, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        cgMaterial.setCreator(token.getUser().getId().toString());
        cgMaterial.setCreateTime(new Date());
        cgMaterial.setEnable(1);
        boolean insert = cgMaterialService.insert(cgMaterial);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody CgMaterial cgMaterial, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        cgMaterial.setCreator(token.getUser().getId().toString());
        cgMaterial.setCreateTime(new Date());
        boolean insert = cgMaterialService.updateById(cgMaterial);
        return  new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryCgMaterial queryCgMaterial, HttpServletRequest request)throws AdscmSystemException {
        Page<CgMaterial> page=new Page(queryCgMaterial.getCurrentPage(),queryCgMaterial.getPageSize());
        page = cgMaterialService.selectCgMaterial(page,queryCgMaterial);
        return  new SimpleCoreResponse("200","修改成功",page);
    }
    @RequestMapping("selectMaterial")
    public SimpleCoreResponse selectMaterial(@RequestBody QueryCgMaterial queryCgMaterial, HttpServletRequest request)throws AdscmSystemException {
        Page<CgMaterial> page=new Page(queryCgMaterial.getCurrentPage(),queryCgMaterial.getPageSize());
        page = cgMaterialService.selectCgMaterialAndSupplier(page,queryCgMaterial);
        return  new SimpleCoreResponse("200","修改成功",page);
    }
    @RequestMapping("selectByKeyWords")
    public SimpleCoreResponse selectByKeyWords( String name, HttpServletRequest request)throws AdscmSystemException {
        List<CgMaterial> CgMaterial=new ArrayList<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(name,null)){
            CgMaterial = cgMaterialService.selectByKeyWords(name);
        }
        return  new SimpleCoreResponse("200","查询成功",CgMaterial);
    }
}

